package com.lanbu.system.service.impl;

import com.lanbu.common.config.RuoYiConfig;
import com.lanbu.common.core.domain.AjaxResult;
import com.lanbu.common.utils.file.FileUploadUtils;
import com.lanbu.system.service.IUploadService;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.lanbu.common.utils.file.FileUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@Service
public class UploadServiceImpl implements IUploadService {


    /**
     * 得到文件所属目录路径
     */
    private String getFileFolderPath(String fileMd5) {
        return fileMd5.substring(0, 1) + "/" + fileMd5.substring(1, 2) + "/" + fileMd5 + "/";
    }

    /**
     * 得到文件的路径
     */
    private String getFilePath( String fileMd5, String fileExt) {
        return  fileMd5.substring(0, 1) + "/" + fileMd5.substring(1, 2) + "/" + fileMd5 + "/" + fileMd5 + "." + fileExt;
    }

    /**
     * 得到块文件所属目录路径
     */
    private String getChunkFileFolderPath( String fileMd5) {
        return fileMd5.substring(0, 1) + "/" + fileMd5.substring(1, 2) + "/" + fileMd5 + "/chunk/";
    }

    @Override
    public String fileRegister(String fileMd5,  String fileExt) throws IOException {
        String uploadPath = RuoYiConfig.getUploadPath()+ "/";
        //检测目录文件是否存在
        String filePath = uploadPath + this.getFilePath(fileMd5, fileExt);
        File file = new File(filePath);
        if (file.exists()) {
            return FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath(), this.getFilePath(fileMd5, fileExt));
        }
        //文件所属目录的路径
        String fileFolderPath =uploadPath + this.getFileFolderPath(fileMd5);
        //文件不存在时作一些准备工作，检查文件所在目录是否存在，如果不存在则创建
        File fileFolder = new File(fileFolderPath);
        if (!fileFolder.exists()) {
            fileFolder.mkdirs();
        }
        return null;
    }

    @Override
    public Boolean checkchunk(String fileMd5, Integer chunk) {
        String uploadPath = RuoYiConfig.getUploadPath()+ "/";
        //得到分块文件的所在目录
        String chunkFileFolderPath = uploadPath + this.getChunkFileFolderPath(fileMd5);
        //块文件
        File chunkFile = new File(chunkFileFolderPath + chunk);

        return chunkFile.exists();
    }

    /**
     * 上传分快
     * @param file
     * @param fileMd5
     * @param chunk
     * @return
     */
    @Override
    public AjaxResult uploadchunk(MultipartFile file, String fileMd5, Integer chunk) {
        if (file.isEmpty()) {
            return AjaxResult.error();
        }
        //检查分块目录，如果不存在则要自动创建
        //得到分块目录
        String uploadPath = RuoYiConfig.getUploadPath() + "/";
        String chunkFileFolderPath = uploadPath + this.getChunkFileFolderPath(fileMd5);
        //得到分块文件路径
        String chunkFilePath = chunkFileFolderPath + chunk;

        File chunkFileFolder = new File(chunkFileFolderPath);
        //如果不存在则要自动创建
        if (!chunkFileFolder.exists()) {
            chunkFileFolder.mkdirs();
        }
        //得到上传文件的输入流
        InputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = file.getInputStream();
            outputStream = new FileOutputStream(new File(chunkFilePath));
            IOUtils.copy(inputStream, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.success();
    }

    @Override
    public String mergechunks(String fileMd5, String fileExt) throws Exception {
        String uploadPath = RuoYiConfig.getUploadPath()+ "/";
        //等带文件上传完毕
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //合并所有分块
        //得到分块文件的属目录
        String chunkFileFolderPath = uploadPath + this.getChunkFileFolderPath(fileMd5);
        File chunkFileFolder = new File(chunkFileFolderPath);
        //分块文件列表
        File[] files = chunkFileFolder.listFiles();
        List<File> fileList = Arrays.asList(files);
        //创建一个合并文件  文件的绝对路径
        String filePath = uploadPath + this.getFilePath(fileMd5, fileExt);
        File mergeFile = new File(filePath);
        //执行合并
        mergeFile = FileUtils.mergeFile(fileList, mergeFile);
        if (mergeFile == null) {
            //合并文件失败
            throw new Exception("合并文件失败");
        }
        FileUtils.deleteDir(chunkFileFolderPath);
        return FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath(), this.getFilePath(fileMd5, fileExt));

    }




}
